package com.csx.governance.sample.consumerC.listener;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author cuisongxu
 * @date 2023/3/5 11:28
 */
@Service
@RocketMQMessageListener(topic = TrafficRocketMqConsumerListener.TRAFFIC_TOPIC, consumerGroup = TrafficRocketMqConsumerListener.CONSUMER_GROUP)
public class TrafficRocketMqConsumerListener implements RocketMQListener<String> {

    public static final String TRAFFIC_TOPIC = "traffic_topic";

    public static final String CONSUMER_GROUP = "traffic_consumer_group";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Registration registration;

    @Override
    public void onMessage(String message) {
        System.out.println("Traffic Consumer receive msg: " + message);
        Map<String, String> metadata = registration.getMetadata();
        String host = registration.getHost();
        int port = registration.getPort();
        String instanceInfo = host + ":" + port + "  metadata: " + metadata.toString();
        String url = "http://serviceD/serviceD/testFullLink";
        String res = restTemplate.getForObject(url, String.class);
        System.out.println("consumerC " + instanceInfo + " ->  \n" + res);
    }
}
